/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.enums.AbnormalEnum;
import com.pig4cloud.pigx.common.customize.exception.DuckweedException;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.user.dto.CapitalAccountPageDTO;
import com.pig4cloud.pigx.user.dto.UpdateCapitalAccountDTO;
import com.pig4cloud.pigx.user.entity.CapitalAccount;
import com.pig4cloud.pigx.user.mapper.CapitalAccountMapper;
import com.pig4cloud.pigx.user.service.CapitalAccountService;
import com.pig4cloud.pigx.user.vo.AdminPageDemoVO;
import com.pig4cloud.pigx.user.vo.CapitalPageVO;
import com.pig4cloud.pigx.user.vo.LoginUserCapitalVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.RoundingMode;
import java.util.List;

/**
 * 资金账户
 *
 * @author pigx code generator
 * @date 2022-11-21 17:40:27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/capital_account" )
@Api(value = "capitalaccount", tags = "资金账户管理")
public class CapitalAccountController {

    private final  CapitalAccountService capitalAccountService;

	private final CapitalAccountMapper mapper;

    /**
     * 分页查询
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page" )
	@PreAuthorize("@pms.hasPermission('user_capitalaccount_view')" )
    public R getCapitalAccountPage(@RequestBody CapitalAccountPageDTO dto) {
		try {
			// 分页
			Page<CapitalPageVO> page = mapper.selectAdminPage(new Page<CapitalAccount>() {{
				setCurrent(dto.getCurrent());
				setSize(dto.getSize());
			}}, dto);
			for (CapitalPageVO record : page.getRecords()) {
				record.setBalance(record.getBalance().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
				record.setFuel(record.getFuel().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
				record.setFrozen(record.getFrozen().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
				record.setContributionValue(record.getContributionValue().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
			}
			// 统计
			CapitalPageVO statistics = mapper.selectAdminStatistics(dto);
			statistics.setFuel(statistics.getFuel().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
			statistics.setBalance(statistics.getBalance().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
			statistics.setFrozen(statistics.getFrozen().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
			statistics.setContributionValue(statistics.getContributionValue().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
			return R.ok(new AdminPageDemoVO<CapitalPageVO>(){{
				setPage(page);
				setStatistics(statistics);
			}});
		} catch (Exception e) {
			throw new DuckweedException(AbnormalEnum.MSG.DISCONNECTED);
		}
    }


    /**
     * 通过id查询资金账户
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_capitalaccount_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(capitalAccountService.getById(id));
    }

    /**
     * 新增资金账户
     * @param capitalAccount 资金账户
     * @return R
     */
    @ApiOperation(value = "新增资金账户", notes = "新增资金账户")
    @SysLog("新增资金账户" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user_capitalaccount_add')" )
    public R save(@RequestBody CapitalAccount capitalAccount) {
        return R.ok(capitalAccountService.save(capitalAccount));
    }

	/**
	 * 人工充值 修改资金账户
	 * @param dto dto
	 * @return r
	 */
    @ApiOperation(value = "人工充值 修改资金账户", notes = "人工充值 修改资金账户")
    @SysLog("人工充值 修改资金账户" )
    @PutMapping
    public R updateById(@RequestBody UpdateCapitalAccountDTO dto) {
        return R.ok(capitalAccountService.updateByAdmin(dto));
    }

    /**
     * 通过id删除资金账户
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除资金账户", notes = "通过id删除资金账户")
    @SysLog("通过id删除资金账户" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_capitalaccount_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(capitalAccountService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param capitalAccount 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('user_capitalaccount_export')" )
    public List<CapitalAccount> export(CapitalAccount capitalAccount) {
        return capitalAccountService.list(Wrappers.query(capitalAccount));
    }

	/**
	 * 获取当前登录人的资金账户
	 * @return r
	 */
	@GetMapping("/app")
	public R<LoginUserCapitalVO> getUserCapital() {
		return capitalAccountService.getUserCapital();
	}
}
